<?php

/**
 *  @FileComment            [前台公共模板]
 *  @ProjectName             项目名称：shopFrame
 *  @FileName                   文件名称：CommonController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-14 23:18:54
 *  *
 */
class CommonController extends Controller {

        public $model;

        public function __init() {
                $this->model = K('Type');
                $this->header();
        }

        public function header() {
                //类型分组
                $typeCache = S('typeCache');
                if (!$typeCache) {
                        $type = $this->model->get_all('tsort');
                        $typeCache = $type;
                        S('typeCache', $type, 3600);
                }
                //缓存处理，文件存储方式
                $this->assign('Type', $typeCache);
                $typeConCache = S('typeCon');
                if (!$typeConCache) {
                        $typeCon = $this->model->get_all();
                        $typeConCache = $typeCon;
                        S('typeCon', $typeCon, 3600);
                }
                $this->assign('typeCon', $typeConCache);
                //获得所有一级和二级分类/首页万年不变的分类
                $category = K('TypeCategoryView');
                $dataCache = S('sonCate');
                if (!$dataCache) {
                        $data = $category->get_all(0);
                        foreach ($data as $k => $v) {
                                $sonData = $category->get_all($v['cid']);
                                $data[$k]['son'] = $sonData;
                        }
                        $dataCache = $data;
                        S('sonCate', $data, 3600);
                }
                $this->assign('data', $dataCache);
                //获得所有品牌
                $brandCache = S('Brand');
                if (!$brandCache) {
                        $brand = K('BrandTypeView')->get_data();
                        $brandCache = $brand;
                        S('Brand', $brand, 3600);
                }
                $this->assign('brand', $brandCache);
                if (session('username') && session('uid')) {
                        $shopCart = K('shopCart');
                        $shopInfo = $shopCart->get_one_data(session('uid'));
                        //获取本地所有购物车session内容
                        $allCart = Cart::getAllData();
                        //如果数据库中没有该用户的session，则插入，否则，则合并数组
                        if (empty($shopInfo)) {
                                $shopCart->add_data($allCart);
                        } else {
                                $shopInfo['getAllData'] = unserialize($shopInfo['getAllData']);
                                //如果数据库里面的购物车session有内容，才插入到本地的session中
                                Cart::delAll();
                                if (!empty($shopInfo['getAllData']['goods'])) {
                                        foreach ($shopInfo['getAllData']['goods'] as $v) {
                                                Cart::add($v);
                                        }
                                }
                        }
                }
                $CartData = Cart::getGoods();
                $allCart = Cart::getAllData();
                $this->assign('cartData', $CartData);
                $this->assign('allCart', $allCart);
        }

        public function otCate($cid) {
                //获得所有一级和二级分类
                $tid = Q('get.tid', 0, 'intval');
                if (!$cid) {
                        $tid = Q('get.tid', 0, 'intval');
                } else {
                        $typeInfo = K('TypeCategoryView')->get_one_data($cid);
                        $tid = $typeInfo['type_tid'];
                }
                $category = K('TypeCategoryView');
                $data = $category->get_tid_all(0, $tid);
                foreach ($data as $k => $v) {
                        $sonData = $category->get_all($v['cid']);
                        $data[$k]['son'] = $sonData;
                }
                $this->assign('otCate', $data);
        }

        public function cate($cid) {
                //nav下面当前在哪个分类
                if (!$cid) {
                        $tid = Q('get.tid', 0, 'intval');
                        $typeInfo = K('Type')->get_one_data($tid);
                } else {
                        $allCate = K('category')->get_all_data();
                        $nowCate = $this->father_cate($allCate, $cid);
                        $typeInfo = K('TypeCategoryView')->get_one_data($cid);
                        $nowCate = array_reverse($nowCate);
                        $this->assign('nowCate', $nowCate);
                }
                $this->assign('type', $typeInfo);
        }

        protected function father_cate($cate, $pid) {
                //获得当前分类的所有父级分类
                $arr = array();
                foreach ($cate as $k => $v) {
                        if ($pid == $v['cid']) {
                                $arr[] = $v;
                                $arr = array_merge($arr, $this->father_cate($cate, $v['pid']));
                        }
                }
                return $arr;
        }

}
